package com.xbongbong.workflow.service.operator.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.help.ProFormHelp;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.ItemUtil;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.saas.enums.ListBusinessTypeEnum;
import com.xbongbong.workflow.domain.entity.WorkflowNodeEntity;
import com.xbongbong.paas.pojo.workflow.WorkflowDataPojo;
import com.xbongbong.workflow.pojo.WorkflowOperationsPojo;
import com.xbongbong.workflow.service.operator.WorkflowOperatorFieldHandler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @Description 工作流更新数据阶段推进器字段处理
 * @Author sunshaoxiang
 * @Date 2021/12/6 14:47
 * @Version 1.0
 */
@Service("WorkflowOperatorStageHandlerImpl")
public class WorkflowOperatorStageHandlerImpl implements WorkflowOperatorFieldHandler {

    @Resource
    private ProFormHelp proFormHelp;
    @Override
    public List<Integer> type() {
        return Arrays.asList(FieldTypeEnum.STAGE_THRUSTER.getType());
    }

    @Override
    public Object handlerThisFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, WorkflowDataPojo workflowDataPojo, WorkflowOperationsPojo workflowOperationsPojo) throws XbbException {
        String corpid = paasFormDataEntity.getCorpid();
        Long formId = paasFormDataEntity.getFormId();
        Integer businessType = workflowOperationsPojo.getBusinessType();
        Integer saasMark = workflowOperationsPojo.getSaasMark();
        List<ItemPoJo> itemPoJos = proFormHelp.getEditStageComboItems(fieldAttrEntity, saasMark, businessType, corpid, formId, data);
        fieldAttrEntity.setItems(itemPoJos);
        return ItemUtil.getCustomItemList(fieldAttrEntity, data);
    }

    @Override
    public Object handlerTargetFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, WorkflowDataPojo workflowDataPojo, WorkflowOperationsPojo workflowOperationsPojo, WorkflowNodeEntity workflowNodeEntity) throws XbbException {
        return null;
    }

    @Override
    public Object handlerFixedValue(FieldAttrEntity targetField, WorkflowDataPojo workflowDataPojo, WorkflowOperationsPojo workflowOperationsPojo, WorkflowNodeEntity workflowNodeEntity) throws XbbException {
        return null;
    }

    @Override
    public Object formulaHandlerThisField(FieldAttrEntity fieldAttrEntity, PaasFormDataEntity formDataEntity, JSONObject data, Long formId, Integer businessType, String corpid) throws XbbException {
        Object stageValue = "";
        Integer saasMark;
        if (Objects.equals(businessType, ListBusinessTypeEnum.CUSTOMER_MANAGEMENT.getBusinessType()) || Objects.equals(businessType, ListBusinessTypeEnum.SALES_OPPORTUNITY.getBusinessType())
            || Objects.equals(businessType, ListBusinessTypeEnum.CLUE.getBusinessType())) {
            saasMark = SaasMarkEnum.SAAS.getCode();
        } else {
            saasMark = SaasMarkEnum.PAAS.getCode();
        }
        List<ItemPoJo> itemPoJos = proFormHelp.getEditStageComboItems(fieldAttrEntity, saasMark, businessType, corpid, formId, data);
        fieldAttrEntity.setItems(itemPoJos);
        stageValue = ItemUtil.getCustomItemList(fieldAttrEntity, data);
        if (Objects.isNull(stageValue)) {
            stageValue = "";
        }
        return "\""+ stageValue + "\"";
    }

    @Override
    public Object formulaHandlerTargetField(Object value, WorkflowOperationsPojo workflowOperationsPojo, FieldAttrEntity targetField, WorkflowNodeEntity workflowNodeEntity, String corpid) throws XbbException {
        return null;
    }
}
